/**************************************************************
 *@ProjectName: blweibo
 *@FileName: SwitchUser.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhiweibo.models.StaticInfo;
import com.zhiweibo.models.User;
import com.zhiweibo.util.Constant;
import com.zhiweibo.util.Helper;
import com.zhiweibo.util.HttpUtil;

/**
 * The Class SwitchUser.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class SwitchUser extends BaseActivity {

    /** The tv user name. */
    private AutoCompleteTextView tvUserName;

    /** The m et password. */
    private EditText mEtPassword;

    /** The wd. */
    private WeiboDB wd;

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("login");
        setView(R.layout.switchuser);
        setTitleBar(1, getString(R.string.imageviewer_back), getString(R.string.app_name), null);
        tvUserName = (AutoCompleteTextView) findViewById(R.id.etLoginUsername);
        mEtPassword = (EditText) findViewById(R.id.etPwd);
        wd = WeiboDB.getInstance(SwitchUser.this);
        ListView mListView = (ListView) findViewById(R.id.lvLinks);
        mListView.setDivider(null);
        mListView.setAdapter(new SquareItemAdapter(this));
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(SwitchUser.this, LookAroundListActivity.class));
            }
        });
        ((Button) findViewById(R.id.bnLogin)).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                new SwitchTask().execute(new Object[0]);
            }
        });
    }

    /**
     * The Class SquareItemAdapter.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class SquareItemAdapter extends BaseAdapter {

        /** The context. */
        Context context;

        /** The tv name. */
        TextView tvName;

        /**
         * Instantiates a new square item adapter.
         * 
         * @param arg2
         *            the arg2
         */
        public SquareItemAdapter(Context arg2) {
            context = arg2;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getCount()
         */
        @Override
        public int getCount() {
            return 1;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getItem(int)
         */
        @Override
        public Object getItem(int paramInt) {
            return Integer.valueOf(paramInt);
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getItemId(int)
         */
        @Override
        public long getItemId(int paramInt) {
            return paramInt;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getView(int, android.view.View,
         * android.view.ViewGroup)
         */
        @Override
        public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
            View localView = ((LayoutInflater) context.getSystemService("layout_inflater")).inflate(
                    R.layout.square_item_view, null);
            tvName = (TextView) localView.findViewById(R.id.tvItemName);
            if (paramInt == 0) {
                tvName.setText(getString(R.string.link_lookaround));
            }
            return localView;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#handleTitleBarEvent(int)
     */
    @Override
    protected void handleTitleBarEvent(int paramInt) {
        switch (paramInt) {
        case 1:
            finish();
            break;
        }
    }

    /**
     * The Class SwitchTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class SwitchTask extends AsyncTask<Object, Integer, User> {

        /** The pd. */
        private ProgressDialog pd;

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected User doInBackground(Object... paramArrayOfString) {
            String strUserName = tvUserName.getText().toString();
            String strPasswd = mEtPassword.getText().toString();
            if (HttpUtil.login(strUserName, strPasswd)) {
                String strUsername = tvUserName.getText().toString();
                String strPassword = mEtPassword.getText().toString();
                Helper.saveString(SwitchUser.this, "username", strUsername);
                Helper.saveString(SwitchUser.this, "password", strPassword);
                StaticInfo.mUsername = strUsername;
                StaticInfo.mPassword = strPassword;
                return HttpUtil.getUserInfo(SwitchUser.this, Constant.USERS_SHOW);
            } else {
                return new User();
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(User user) {
            if (TextUtils.isEmpty(user.getUid())) {
                Toast.makeText(SwitchUser.this, R.string.login_failed, Toast.LENGTH_SHORT).show();
            } else {
                StaticInfo.user = user;
                StaticInfo.mNickName = user.getUsername();
                StaticInfo.mUid = user.getUid();
                Helper.saveString(SwitchUser.this, "nickname", StaticInfo.mNickName);
                Helper.saveString(SwitchUser.this, "uid", StaticInfo.mUid);
                AccountHelper.add(SwitchUser.this);
                startActivity(new Intent(SwitchUser.this, MainTabActivity.class));
                wd.deleteAllTopic();
                wd.deleteAllAt();
                wd.deleteAllComment();
                wd.deleteAllMessage();
                wd.deleteUser();
                wd.insertUser(user);
                StaticInfo.cleanDate();
                Helper.delString(SwitchUser.this, "newId");
                finish();
            }
            if (pd != null) {
                pd.dismiss();
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            pd = new ProgressDialog(SwitchUser.this);
            pd.setMessage("登陆中……");
            pd.show();
        }

    }
}
